package com.focus.xyz.other.pages.pingan;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by chenmeng on 2016/6/29.
 */
public class HomePage extends BasePage {
    private Action action;
    private WebDriver driver;

    private static final String mainUrl1 = "https://icorepnbs.pingan.com.cn/icore_pnbs/";
    private static final String mainUrl = "https://icorepnbs.pingan.com.cn/icore_pnbs/extranet_login.jsp";

    @FindBy(id = "j_username")
    private WebElement username;
    @FindBy(id = "j_password")
    private WebElement password;
    @FindBy(id = "Submit")
    private WebElement loginBtn;

    @FindBy(id = "deptCodeText")
    private WebElement deptCodeText;
    @FindBy(id = "employeeCodeText")
    private WebElement employeeCodeText;
    @FindBy(xpath = "//button[@class='btn btn-primary w80']")
    private WebElement confirmBtn;
    @FindBy(xpath = "//label[@class='control-label']")
    private WebElement blankPlace;

    /*
    * 投保跟踪菜单
    * */
    @FindBy(xpath = "//a[@id='beApply']")
    private WebElement traceMenu;


    public HomePage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        this.driver = driver;
        super.url = mainUrl;
        PageFactory.initElements(this.driver,this);
    }

    public void switchMain() throws Exception{
        driver.switchTo().frame("main");
    }

    public void switchDefault() throws Exception{
        driver.switchTo().defaultContent();
    }

    public void clickTraceMenu() throws Exception{
        action.click(traceMenu);
        action.waitForPageLoad();
    }

    public boolean isLogined() throws Exception{
        boolean flag = false;
        if(action.isElementExsit(By.xpath("//*[@id=\"j_username\"]"))){
            flag = false;
        }else{
            flag = true;
        }
        return flag;
    }

    public void login(String userName,String pass) throws Exception {
        action.inputText(username,userName);
        action.inputText(password,pass);
        action.click(loginBtn);
        action.waitForPageLoad();
    }

    public void newOffer(String orgCode,String empCode) throws Exception{
        action.inputText(deptCodeText,orgCode);
        action.click(blankPlace);
        action.waitForAjax();
        action.pause(2000l);
        action.inputText(employeeCodeText,empCode);
        action.click(blankPlace);
        action.waitForAjax();
        action.pause(2000l);
        action.click(confirmBtn);
    }
}
